package javabasics.concurrent.guardedsuspension;

import java.util.LinkedList;

/**
 * @Author suxiaoxiao
 * @Date 2019/6/16 13:36
 * @Version 1.0
 */
public class RequestQueue {

    private LinkedList queue = new LinkedList();

    // 返回队列中首位请求
    public synchronized Request getReqeust() {
        while (queue.size() == 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return (Request) queue.remove();
    }

    public synchronized void addRequest(Request request) {
        // 加入新的 Request 请求
        queue.add(request);
        // 通知 getRquest() 方法
        notifyAll();
    }

}
